** I/B/E/S 1yr forecast


clear all

local InputDataDir = "...\IBES" // IBES file requires WRDS access

local OutputDir = "...\results" // results path




cd "`InputDataDir'"
use IBES1yr

gen yyyymm=ACTDATS
format yyyymm %tdCCYYNN
tostring yyyymm, generate(yyyymm1) force usedisplayformat
destring yyyymm1, replace float
drop yyyymm
rename yyyymm1 yyyymm



save IBES1yr_temp, replace

use IBES_CRSP_Link, clear
duplicates drop NCUSIP PERMNO, force
rename NCUSIP CUSIP
drop if PERMNO == .

merge 1:m CUSIP using IBES1yr_temp

order TICKER PERMNO CUSIP yyyymm sdate edate 
keep if _merge == 3
drop _merge
sort CUSIP yyyymm sdate edate

erase IBES1yr_temp.dta

merge m:m PERMNO yyyymm using BM_SIZE_PRC
drop if _merge==1
drop if _merge==2
drop _merge

bysort PERMNO yyyy : egen median_forecast=median(VALUE)
order PERMNO yyyymm ANALYS VALUE ACTUAL median_forecast 

replace PRC = -PRC if PRC < 0

g EarningSurprises_1yr = (ACTUAL - median_forecast)/ PRC
drop if EarningSurprises_1yr > 0.1
drop if EarningSurprises_1yr < -0.1

replace EarningSurprises_1yr = EarningSurprises_1yr*1000

gen yyyy=date
format yyyy %tdCCYY
tostring yyyy, generate(yyyy1) force usedisplayformat
destring yyyy1, replace float
drop yyyy
rename yyyy1 yyyy


gen ActualtoForecast_8month = round((FPEDATS-ACTDATS)/30)
keep if ActualtoForecast_8month == 8 

duplicates drop PERMNO yyyymm, force

cd "`OutputDir'"

replace SIZE = SIZE/100000000
label var ln_BM "BM"
label var bb "BB"
*xtset PERMNO yyyymm

regress EarningSurprises_1yr bb i.yyyy i.IndNum, cluster(PERMNO)
est sto M_YearIndFE
regress EarningSurprises_1yr bb ln_BM i.yyyy i.IndNum, cluster(PERMNO)
est sto M_YearIndFE
regress EarningSurprises_1yr bb ln_BM SIZE i.yyyy i.IndNum, cluster(PERMNO)
est sto model5
outreg2 [*] using Reg_IBES1yr.doc, replace stats(coef tstat) dec(2) keep(bb ln_BM SIZE) nocons label



** I/B/E/S 2yr forecast

cd "`InputDataDir'"
use IBES2yr, clear

gen yyyymm=ACTDATS
format yyyymm %tdCCYYNN
tostring yyyymm, generate(yyyymm1) force usedisplayformat
destring yyyymm1, replace float
drop yyyymm
rename yyyymm1 yyyymm



save IBES2yr_temp, replace

use IBES_CRSP_Link, clear
duplicates drop NCUSIP PERMNO, force
rename NCUSIP CUSIP
drop if PERMNO == .

merge 1:m CUSIP using IBES2yr_temp

order TICKER PERMNO CUSIP yyyymm sdate edate 
keep if _merge == 3
drop _merge
sort CUSIP yyyymm sdate edate

erase IBES2yr_temp.dta

merge m:m PERMNO yyyymm using BM_SIZE_PRC
drop if _merge==1
drop if _merge==2
drop _merge

bysort PERMNO yyyy : egen median_forecast=median(VALUE)
order PERMNO yyyymm ANALYS VALUE ACTUAL median_forecast 

replace PRC = -PRC if PRC < 0

g EarningSurprises_2yr = (ACTUAL - median_forecast)/ PRC
drop if EarningSurprises_2yr > 0.1
drop if EarningSurprises_2yr < -0.1

replace EarningSurprises_2yr = EarningSurprises_2yr*1000

gen yyyy=date
format yyyy %tdCCYY
tostring yyyy, generate(yyyy1) force usedisplayformat
destring yyyy1, replace float
drop yyyy
rename yyyy1 yyyy


gen ActualtoForecast_20month = round((FPEDATS-ACTDATS)/30)
keep if ActualtoForecast_20month == 20 

duplicates drop PERMNO yyyymm, force

cd "`OutputDir'"

replace SIZE = SIZE/100000000
label var ln_BM "BM"
label var bb "BB"
*xtset PERMNO yyyymm
regress EarningSurprises_2yr bb i.yyyy i.IndNum, cluster(PERMNO)
est sto M_YearIndFE
regress EarningSurprises_2yr bb ln_BM i.yyyy i.IndNum, cluster(PERMNO)
est sto M_YearIndFE
regress EarningSurprises_2yr bb ln_BM SIZE i.yyyy i.IndNum, cluster(PERMNO)
est sto model5
outreg2 [*] using Reg_IBES2yr.doc, replace stats(coef tstat) dec(2) keep(bb ln_BM SIZE) nocons label



** I/B/E/S 5yr forecast

cd "`InputDataDir'"
use IBES5yr, clear

gen yyyymm=ACTDATS
format yyyymm %tdCCYYNN
tostring yyyymm, generate(yyyymm1) force usedisplayformat
destring yyyymm1, replace float
drop yyyymm
rename yyyymm1 yyyymm



save IBES5yr_temp, replace

use IBES_CRSP_Link, clear
duplicates drop NCUSIP PERMNO, force
rename NCUSIP CUSIP
drop if PERMNO == .

merge 1:m CUSIP using IBES5yr_temp

order TICKER PERMNO CUSIP yyyymm sdate edate 
keep if _merge == 3
drop _merge
sort CUSIP yyyymm sdate edate

erase IBES5yr_temp.dta

merge m:m PERMNO yyyymm using BM_SIZE_PRC
drop if _merge==1
drop if _merge==2
drop _merge

bysort PERMNO yyyy : egen median_forecast=median(VALUE)
order PERMNO yyyymm ANALYS VALUE ACTUAL median_forecast 

replace PRC = -PRC if PRC < 0

g EarningSurprises_5yr = ACTUAL - median_forecast
drop if EarningSurprises_5yr > 10
drop if EarningSurprises_5yr < -10

replace EarningSurprises_5yr = EarningSurprises_5yr*10

gen yyyy=date
format yyyy %tdCCYY
tostring yyyy, generate(yyyy1) force usedisplayformat
destring yyyy1, replace float
drop yyyy
rename yyyy1 yyyy


gen ActualtoForecast_56month = round((FPEDATS-ACTDATS)/30)
keep if ActualtoForecast_56month >= 56 

duplicates drop PERMNO yyyymm, force

cd "`OutputDir'"

replace SIZE = SIZE/100000000
label var ln_BM "BM"
label var bb "BB"
*xtset PERMNO yyyymm

regress EarningSurprises_5yr bb i.yyyy i.IndNum, robust
est sto M_YearIndFE
regress EarningSurprises_5yr bb ln_BM i.yyyy i.IndNum, robust
est sto M_YearIndFE
regress EarningSurprises_5yr bb ln_BM SIZE i.yyyy i.IndNum, robust
est sto model5
outreg2 [*] using Reg_IBES5yr.doc, replace stats(coef tstat) dec(2) keep(bb ln_BM SIZE) nocons label



